package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import util.MyUtil;
@WebServlet("/uploadHttpOneServlet")
@MultipartConfig(maxFileSize = 30*1024)
public class UploadHttpOneServlet extends HttpServlet{
    private static final long serialVersionUID=1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        request.setCharacterEncoding("utf-8");
        Part part=request.getPart("resPath");
        String filediscription=request.getParameter("filediscription");
        out.println("文件的描述："+filediscription+"<br>");
        File uploadFileDir=new File(getServletContext().getRealPath("/uploadFiles"));
        if(!uploadFileDir.exists())
            uploadFileDir.mkdir();
        String oldName = MyUtil.getFileName(part);
        oldName=oldName.substring(oldName.lastIndexOf("\\")+1,oldName.length());
        out.println("上传文件的原始名：" + oldName + "<br>");
        out.println("上传文件的大小：" + part.getSize() + "<br>");
        if (oldName != null) {
            //真正上传的操作，将文件上传到服务器之前指定的uploadFileDir目录下
            part.write(uploadFileDir + File.separator + oldName);
        }
        out.println("文件上传到：" + uploadFileDir + File.separator + oldName + "<br>");
    }
}
